package org.s3.project.controller;

import org.s3.project.entity.Favorites;
import org.s3.project.service.FavoritesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ljh
 * @version 1.0
 * @Description
 * @date 2025/2/18 09:14
 */
@CrossOrigin(origins = "*")
@RestController
public class FavoritesController {
    @Autowired
    private FavoritesService favoritesService;

    //新增收藏
    @PostMapping("getFavoritesAdd")
    public Integer getFavoritesAdd(@RequestBody Favorites favorites){
        Integer a = favoritesService.selectFavoritesByUserIdAndDishId(favorites);
        if (a==0) {//没有收藏过
            favorites.setCreate_time(new java.sql.Timestamp(System.currentTimeMillis()));
            favorites.setUpdate_time(new java.sql.Timestamp(System.currentTimeMillis()));
            favoritesService.getFavoritesAdd(favorites);
            return 0;
        }else{
            System.out.println(favorites.getDish_id());
            Integer user_id = (int) favorites.getUser_id();
            Integer dish_id = (int) favorites.getDish_id();
            favoritesService.deleteFavorites(user_id,dish_id);
            return 1;
        }
    }

    //通过用户id查询收藏
    @GetMapping("selectFavorites")
    public List<Favorites> selectFavorites(int id){
        return favoritesService.selectFavorites(id);
    }
    //删除收藏
    @GetMapping("deleteFavorites/{user_id}/{dish_id}")
    public int deleteFavorites(@PathVariable("user_id") Integer user_id, @PathVariable("dish_id")Integer dish_id){
        return favoritesService.deleteFavorites(user_id,dish_id);
    }

    //查询收藏数量
    @GetMapping("selectFavoritesCount/{user_id}")
    public List<Favorites> selectFavoritesCount(@PathVariable("user_id") int user_id){
        return favoritesService.selectFavoritesCount(user_id);
    }
    //查询以收藏id
    @GetMapping("getFavoritesId/{user_id}")
    public List<Favorites> getFavoritesId(@PathVariable("user_id") int user_id){
        return favoritesService.getFavoritesId(user_id);
    }

}
